/**
 * Copyright (c) 2016,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.base.utils;

import com.cargo.base.exceptions.CommonException;
import com.cargo.base.mail.Mail;
import com.cargo.base.mail.Sendmail;
import com.cargo.base.mail.Smtp;

/**
 * MailSender:发邮件
 * 
 * @Project Name:cargo
 * @File Name:MailSender.java
 * @Package Name:com.cargo.utils
 * @Creator:wizard
 * @Date:2016年4月14日下午5:41:26
 */
public class MailSender {
	private static Smtp smtp = new Smtp();
	private static Mail mail = new Mail();

	/**
	 * 
	 * send:(系统配置邮件发送方法).
	 * @param subject：邮件标题
	 * @param content：邮件内容
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年11月1日 下午2:12:33
	 */
	public static void send(String subject, String content) {
		mail.subject = subject;
		mail.content = content;
		try {
			Sendmail.send(smtp, mail);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * send:(自定义邮件发送方法).
	 * @param host：邮件传送协议服务器地址
	 * @param port：邮件传送协议服务器端口为空
	 * @param username：邮件传送协议服务器用户名为空
	 * @param password：邮件传送协议服务器密码为空
	 * @param from：邮件发送方地址为空
	 * @param to：邮件接收方地址为空
	 * @param subject：邮件标题
	 * @param content：邮件内容
	 * @throws Exception 
	 * @return :void 
	 * @Creator:wizard
	 * @Date:2016年11月1日 下午2:12:09
	 */
	public static void send(String host,String port,String username,String password,String from,String to,String subject, String content)throws Exception {
		try {
			
			Smtp smtp = new Smtp();
			Mail mail = new Mail();
			
			if(CheckUtil.checkObj(host)){
				smtp.host = host;
			}else{
				throw new CommonException("邮件传送协议服务器地址为空");
			}
			
			if(CheckUtil.checkObj(port)){
				smtp.port = port;
			}else{
				throw new CommonException("邮件传送协议服务器端口为空");
			}
			
			if(CheckUtil.checkObj(username)){
				smtp.username = username;
			}else{
				throw new CommonException("邮件传送协议服务器用户名为空");
			}
			
			if(CheckUtil.checkObj(password)){
				smtp.password = password;
			}else{
				throw new CommonException("邮件传送协议服务器密码为空");
			}
			
			if(CheckUtil.checkObj(from)){
				mail.from = from;
			}else{
				throw new CommonException("邮件发送方地址为空");
			}
			
			if(CheckUtil.checkObj(to)){
				mail.to = to;
			}else{
				throw new CommonException("邮件接收方地址为空");
			}
			
			mail.subject = subject;
			mail.content = content;
			
			Sendmail.send(smtp, mail);
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommonException(e.getMessage());
		}
	}
}
